// pages/companyManage/companyManage.js
import ajax from "../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageEntity: null,
    pageRoles: 0,
    dataList: [],
    formData: {
      search: '',
      pageSize: 10,
      pageNum: 1,
      total: 0,
    },
    userInfo: null,
    showLoginPicker: false,
  },
  loginSuccess() {
    this.loginCancel()
    this.getUserInfo()
  },
  loginCancel() {
    this.setData({ showLoginPicker: false })
  },
  isModeEvent() {
    this.setData({ showLoginPicker: true })
  },
  inputChange(event) {
    this.data.formData.search = event.detail.value
  },
  inputConfirm() {
    this.data.formData.pageNum = 1;
    if (this.data.pageRoles == 1) this.getRequestList();
    this.getUserInfo()
  },
  async getRequestList() {
    const { formData, dataList } = this.data
    const res = await ajax.get('/enterprise/manage/list', formData).finally(() => wx.stopPullDownRefresh());
    this.data.formData.total = res.total
    this.setData({ dataList: formData.pageNum == 1 ? res.rows : dataList.concat(res.rows) })
  },
  async getUserInfo() {
    const res = await ajax.getUserInfo({ isLogin: true })
    this.setData({ userInfo: res })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const pageRoles = wx.getStorageSync('pageRoles') || 0;
    const pageEntity = this.selectComponent('#pageTask')
    this.setData({ pageRoles, pageEntity })
    if (pageRoles == 0) pageEntity.getMarqueeList();
    this.inputConfirm()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.data.pageRoles == 0) return setTimeout(() => wx.stopPullDownRefresh(), 500)
    this.setData({
      'formData.search': ''
    })
    this.data.formData.pageNum = 1;
    this.getRequestList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.pageRoles == 0) return
    const { formData, dataList } = this.data
    if (formData.total == dataList.length) return
    this.data.formData.pageNum += 1;
    this.getRequestList()
  },
  /**
    * 朋友圈分享
    */
  onShareTimeline: function () {
    const shareId = this.data.userInfo?.id || ''
    return {
      title: this.data.datas.gName,
      query: `shareId=${shareId}`,
      imageUrl: 'https://file.yledanpei.com/2021/09/23/414e36f2-8d60-4829-bfce-9b47d1a95ebd.png'
    }
  },

  /**
   * 用户点击右上角分享
   * type 0企业分享 1园区分享 2合伙人分享
   */
  onShareAppMessage: function (event) {
    const type = event.target.dataset.type
    const { id, nickName } = this.data.userInfo
    let url = '', title = '', imageUrl = ''
    if (type == 0) {
      imageUrl = 'https://file.yledanpei.com/2021/09/23/414e36f2-8d60-4829-bfce-9b47d1a95ebd.png'
      title = `${nickName}-邀请您企业入驻`
      url = '/pagesEnterprise/pages/enterpriseAuthentication/enterpriseAuthentication'
    } else if (type == 1) {
      imageUrl = 'https://file.yledanpei.com/2021/09/23/6816582a-5403-4f36-8615-179edc479b3f.png'
      title = `${nickName}-邀请您园区入驻`
      url = "/pagesEnterprise/pages/parkSign/parkSign"
    } else if (type == 2) {
      imageUrl = 'https://file.yledanpei.com/2021/09/23/464ef731-d92b-47f6-ba59-668a9172527d.png'
      title = `${nickName}-邀请您加盟合伙人`
      url = "/pagesMine/pages/cityPartner/cityPartner"
    }
    return {
      title, imageUrl, path: `${url}?shareId=${id}`
    }
  }
})